speaker2 2.2.1
Loading...
Searching...
No Matches
speaker2.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2026 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
27
28#ifndef SPEAKER2_H
29#define SPEAKER2_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_spi_master.h"
52#include "spi_specifics.h"
53
59
64
69
74#define SPEAKER2_CMD_PLAY_VP 0xA6
75#define SPEAKER2_CMD_PLAY_VP_RN 0xAE
76#define SPEAKER2_CMD_PLAY_VP_LP 0xA4
77#define SPEAKER2_CMD_PLAY_VP_RN_LP 0xB2
78#define SPEAKER2_CMD_EXE_VM 0xB0
79#define SPEAKER2_CMD_EXE_VM_RN 0xBC
80#define SPEAKER2_CMD_PLAY_SIL 0xA8
81#define SPEAKER2_CMD_STOP 0x2A
82#define SPEAKER2_CMD_STOP_LP 0x2E
83#define SPEAKER2_CMD_SPI_PCM_READ 0xAC
84#define SPEAKER2_CMD_SPI_SND_DEC 0xC0
85#define SPEAKER2_CMD_READ_STATUS 0x40
86#define SPEAKER2_CMD_READ_INT 0x46
87#define SPEAKER2_CMD_READ_ID 0x48
88#define SPEAKER2_CMD_DIG_READ 0xA2
89#define SPEAKER2_CMD_DIG_WRITE 0xA0
90#define SPEAKER2_CMD_ERASE_MEM 0x24
91#define SPEAKER2_CMD_CHIP_ERASE 0x26
92#define SPEAKER2_CMD_CHECKSUM 0xF2
93#define SPEAKER2_CMD_PWR_UP 0x10
94#define SPEAKER2_CMD_PWR_DN 0x12
95#define SPEAKER2_CMD_SET_CLK_CFG 0xB4
96#define SPEAKER2_CMD_RD_CLK_CFG 0xB6
97#define SPEAKER2_CMD_WR_CFG_REG 0xB8
98#define SPEAKER2_CMD_RD_CFG_REG 0xBA
99#define SPEAKER2_CMD_RESET 0x14
100
105#define SPEAKER2_REG_SAMPLE_RATE_OVERWRITE 0x00
106#define SPEAKER2_REG_DECOMPRESSION_CONTROL 0x01
107#define SPEAKER2_REG_PATH_CONTROL 0x02
108#define SPEAKER2_REG_VOLUME_CONTROL 0x03
109#define SPEAKER2_REG_CHECKSUM_RESET 0x04
110#define SPEAKER2_REG_THERMAL_CONTROL 0x05
111#define SPEAKER2_REG_MASK_JUMP_CONTROL 0x08
112#define SPEAKER2_REG_PWM_CONTROL 0x09
113#define SPEAKER2_REG_CH_CONTROL 0x0C
114#define SPEAKER2_REG_CH0_COUNTER_CONTROL 0x0D
115#define SPEAKER2_REG_CH1_COUNTER_CONTROL 0x0E
116#define SPEAKER2_REG_CH2_COUNTER_CONTROL 0x0F
117#define SPEAKER2_REG_CHECKSUM_1_LB 0x10
118#define SPEAKER2_REG_CHECKSUM_1_HB 0x11
119#define SPEAKER2_REG_CHECKSUM_2_LB 0x12
120#define SPEAKER2_REG_CHECKSUM_2_HB 0x13
121#define SPEAKER2_REG_GPIO_TRIGGER_CH_SEL_1 0x14
122#define SPEAKER2_REG_GPIO_TRIGGER_CH_SEL_2 0x15
123#define SPEAKER2_REG_TRIGGER_VOLUME_CONTROL 0x16
124#define SPEAKER2_REG_DEBOUNCE_TIME_CONTROL 0x17
125#define SPEAKER2_REG_OUTPUT_DATA_CONTROL 0x19
126#define SPEAKER2_REG_OUTPUT_ENABLE_CONTROL 0x1A
127#define SPEAKER2_REG_PULL_ENABLE_CONTROL 0x1B
128#define SPEAKER2_REG_INPUT_DATA_CONTROL 0x1C
129#define SPEAKER2_REG_PULL_SELECT_CONTROL 0x1D
130#define SPEAKER2_REG_ALT_FUNCTION_CONTROL_1 0x1E
131#define SPEAKER2_REG_ALT_FUNCTION_CONTROL_0 0x1F
132#define SPEAKER2_REG_R0_L 0x20
133#define SPEAKER2_REG_R0_H 0x21
134#define SPEAKER2_REG_R1_L 0x22
135#define SPEAKER2_REG_R1_H 0x23
136#define SPEAKER2_REG_R2_L 0x24
137#define SPEAKER2_REG_R2_H 0x25
138#define SPEAKER2_REG_R3_L 0x26
139#define SPEAKER2_REG_R3_H 0x27
140#define SPEAKER2_REG_R4_L 0x28
141#define SPEAKER2_REG_R4_H 0x29
142#define SPEAKER2_REG_R5_L 0x2A
143#define SPEAKER2_REG_R5_H 0x2B
144#define SPEAKER2_REG_R6_L 0x2C
145#define SPEAKER2_REG_R6_H 0x2D
146#define SPEAKER2_REG_R7_L 0x2E
147#define SPEAKER2_REG_R7_H 0x2F
148 // speaker2_cmd
150
155
160
165#define SPEAKER2_STATUS_PD 0x80
166#define SPEAKER2_STATUS_DBUF_RDY 0x40
167#define SPEAKER2_STATUS_INT 0x20
168#define SPEAKER2_STATUS_CH2_BSY 0x08
169#define SPEAKER2_STATUS_CH1_BSY 0x04
170#define SPEAKER2_STATUS_CH0_BSY 0x02
171#define SPEAKER2_STATUS_DIG_BSY 0x01
172#define SPEAKER2_STATUS_CLEAR 0x00
173
178#define SPEAKER2_INT_STATUS_TALARM_INT 0x80
179#define SPEAKER2_INT_STATUS_MPT_ERR 0x40
180#define SPEAKER2_INT_STATUS_WR_FIN 0x20
181#define SPEAKER2_INT_STATUS_CMD_ERR 0x10
182#define SPEAKER2_INT_STATUS_OVF_ERR 0x08
183#define SPEAKER2_INT_STATUS_CH2_FIN 0x04
184#define SPEAKER2_INT_STATUS_CH1_FIN 0x02
185#define SPEAKER2_INT_STATUS_CH0_FIN 0x01
186
191#define SPEAKER2_DEVICE_ID_PART 0x05
192#define SPEAKER2_DEVICE_ID_MAN 0xEF
193#define SPEAKER2_DEVICE_ID_MEM_TYPE 0x20
194#define SPEAKER2_DEVICE_ID_DEV 0x60
195
200#define SPEAKER2_GPIO_AF1_IO 0
201#define SPEAKER2_GPIO_AF1_AF 0
202#define SPEAKER2_GPIO_AF1_FT 1
203#define SPEAKER2_GPIO_AF1_RFT 1
204#define SPEAKER2_GPIO_AF0_IO 0
205#define SPEAKER2_GPIO_AF0_AF 1
206#define SPEAKER2_GPIO_AF0_FT 0
207#define SPEAKER2_GPIO_AF0_RFT 1
208#define SPEAKER2_GPIO_0_POS 0
209#define SPEAKER2_GPIO_1_POS 1
210#define SPEAKER2_GPIO_2_POS 2
211#define SPEAKER2_GPIO_3_POS 3
212#define SPEAKER2_GPIO_4_POS 4
213#define SPEAKER2_GPIO_5_POS 5
214
219#define SPEAKER2_VM0_POI 0
220#define SPEAKER2_VM1_PU 1
221#define SPEAKER2_VM2_WAKEUP 2
222#define SPEAKER2_VM3_ONE 3
223#define SPEAKER2_VM4_TWO 4
224#define SPEAKER2_VM5_THREE 5
225#define SPEAKER2_VM6_FOUR 6
226#define SPEAKER2_VM7_FIVE 7
227#define SPEAKER2_VM8_SIX 8
228#define SPEAKER2_VP9_ONE 9
229#define SPEAKER2_VP10_TWO 10
230#define SPEAKER2_VP11_THREE 11
231#define SPEAKER2_VP12_FOUR 12
232#define SPEAKER2_VP13_FIVE 13
233#define SPEAKER2_VP14_SIX 14
234#define SPEAKER2_VP15_FAST_BEEP 15
235
240#define SPEAKER2_FLASH_START 0x000000ul
241#define SPEAKER2_FLASH_END 0x03FFFFul
242
247#define SPEAKER2_PAYLOAD_SIZE 256
248
253#define SPEAKER2_TIMEOUT_10S 10000
254
263#define SPEAKER2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
264#define SPEAKER2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
265 // speaker2_set
267
272
277
282#define SPEAKER2_MAP_MIKROBUS( cfg, mikrobus ) \
283 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
284 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
285 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
286 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
287 cfg.rdy = MIKROBUS( mikrobus, MIKROBUS_AN ); \
288 cfg.io5 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
289 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
290 // speaker2_map // speaker2
293
298typedef struct
299{
300 uint8_t part_id;
301 uint8_t man_id;
302 uint8_t mem_type;
303 uint8_t dev_id;
304
306
311typedef struct
312{
313 // Output pins
314 digital_out_t io5;
315
316 // Input pins
317 digital_in_t rdy;
318 digital_in_t int_pin;
319
320 // Modules
321 spi_master_t spi;
322
323 pin_name_t chip_select;
324
326 uint8_t status;
327
329
330} speaker2_t;
331
336typedef struct
337{
338 // Communication gpio pins
339 pin_name_t miso;
340 pin_name_t mosi;
341 pin_name_t sck;
342 pin_name_t cs;
343
344 // Additional gpio pins
345 pin_name_t rdy;
346 pin_name_t io5;
347 pin_name_t int_pin;
348
349 // static variable
350 uint32_t spi_speed;
351 spi_master_mode_t spi_mode;
352 spi_master_chip_select_polarity_t cs_polarity;
353
355
360typedef enum
361{
364
366
372
383
398
412
427err_t speaker2_send_command ( speaker2_t *ctx, uint8_t cmd, uint8_t *payload, uint16_t payload_len );
428
438void speaker2_set_io5_pin ( speaker2_t *ctx, uint8_t state );
439
449
459
469
480err_t speaker2_wait_status ( speaker2_t *ctx, uint8_t mask, uint8_t state );
481
491
501
511err_t speaker2_read_status ( speaker2_t *ctx, uint8_t *int_status );
512
522err_t speaker2_clear_interrupt ( speaker2_t *ctx, uint8_t *int_status );
523
534
544
554
564
574err_t speaker2_memory_erase ( speaker2_t *ctx, uint32_t address );
575
585
597err_t speaker2_flash_write ( speaker2_t *ctx, uint32_t address, uint8_t *data_in, uint16_t data_len );
598
610err_t speaker2_flash_read ( speaker2_t *ctx, uint32_t address, uint8_t *data_out, uint16_t data_len );
611
625err_t speaker2_write_binary ( speaker2_t *ctx, uint32_t address, const uint8_t *data_in, uint16_t data_len );
626
637err_t speaker2_write_cfg_reg ( speaker2_t *ctx, uint8_t reg, uint8_t data_in );
638
649err_t speaker2_read_cfg_reg ( speaker2_t *ctx, uint8_t reg, uint8_t *data_out );
650
660err_t speaker2_set_clock_cfg ( speaker2_t *ctx, uint8_t clk_cfg );
661
671err_t speaker2_read_clock_cfg ( speaker2_t *ctx, uint8_t *clk_cfg );
672
684err_t speaker2_spi_audio_write ( speaker2_t *ctx, uint8_t *data_in, uint16_t data_len );
685
697err_t speaker2_pcm_audio_read ( speaker2_t *ctx, uint16_t *data_out, uint16_t data_len );
698
708err_t speaker2_play_voice ( speaker2_t *ctx, uint16_t index );
709
720err_t speaker2_play_voice_loop ( speaker2_t *ctx, uint16_t index, uint16_t loop_cnt );
721
731err_t speaker2_play_macro ( speaker2_t *ctx, uint16_t index );
732
742err_t speaker2_play_silence ( speaker2_t *ctx, uint8_t len );
743
753
763
764#ifdef __cplusplus
765}
766#endif
767#endif // SPEAKER2_H
768 // speaker2
770
771// ------------------------------------------------------------------------ END
#define SPEAKER2_PAYLOAD_SIZE
Speaker 2 cmd payload size setting.
Definition speaker2.h:247
err_t speaker2_power_down(speaker2_t *ctx)
Speaker 2 power down function.
err_t speaker2_wait_idle(speaker2_t *ctx)
Speaker 2 wait idle function.
void speaker2_cfg_setup(speaker2_cfg_t *cfg)
Speaker 2 configuration object setup function.
err_t speaker2_check_communication(speaker2_t *ctx)
Speaker 2 check communication function.
err_t speaker2_default_cfg(speaker2_t *ctx)
Speaker 2 default configuration function.
err_t speaker2_play_silence(speaker2_t *ctx, uint8_t len)
Speaker 2 play silence function.
err_t speaker2_flash_read(speaker2_t *ctx, uint32_t address, uint8_t *data_out, uint16_t data_len)
Speaker 2 flash read function.
err_t speaker2_pcm_audio_read(speaker2_t *ctx, uint16_t *data_out, uint16_t data_len)
Speaker 2 PCM audio read function.
err_t speaker2_wait_status(speaker2_t *ctx, uint8_t mask, uint8_t state)
Speaker 2 wait status function.
err_t speaker2_memory_erase(speaker2_t *ctx, uint32_t address)
Speaker 2 memory erase function.
err_t speaker2_read_cfg_reg(speaker2_t *ctx, uint8_t reg, uint8_t *data_out)
Speaker 2 read configuration register function.
uint8_t speaker2_get_rdy_pin(speaker2_t *ctx)
Speaker 2 get RDY pin function.
err_t speaker2_init(speaker2_t *ctx, speaker2_cfg_t *cfg)
Speaker 2 initialization function.
err_t speaker2_send_command(speaker2_t *ctx, uint8_t cmd, uint8_t *payload, uint16_t payload_len)
Speaker 2 send command function.
err_t speaker2_stop_play(speaker2_t *ctx)
Speaker 2 stop play function.
err_t speaker2_read_status(speaker2_t *ctx, uint8_t *int_status)
Speaker 2 read status function.
err_t speaker2_spi_audio_write(speaker2_t *ctx, uint8_t *data_in, uint16_t data_len)
Speaker 2 SPI audio write function.
err_t speaker2_play_voice(speaker2_t *ctx, uint16_t index)
Speaker 2 play voice function.
void speaker2_set_io5_pin(speaker2_t *ctx, uint8_t state)
Speaker 2 set IO5 pin function.
err_t speaker2_play_macro(speaker2_t *ctx, uint16_t index)
Speaker 2 play macro function.
err_t speaker2_play_voice_loop(speaker2_t *ctx, uint16_t index, uint16_t loop_cnt)
Speaker 2 play voice loop function.
err_t speaker2_write_cfg_reg(speaker2_t *ctx, uint8_t reg, uint8_t data_in)
Speaker 2 write configuration register function.
err_t speaker2_write_binary(speaker2_t *ctx, uint32_t address, const uint8_t *data_in, uint16_t data_len)
Speaker 2 write binary function.
err_t speaker2_clear_interrupt(speaker2_t *ctx, uint8_t *int_status)
Speaker 2 clear interrupt function.
uint8_t speaker2_get_int_pin(speaker2_t *ctx)
Speaker 2 get INT pin function.
err_t speaker2_reset_device(speaker2_t *ctx)
Speaker 2 reset device function.
err_t speaker2_power_up(speaker2_t *ctx)
Speaker 2 power up function.
err_t speaker2_read_clock_cfg(speaker2_t *ctx, uint8_t *clk_cfg)
Speaker 2 read clock configuration function.
err_t speaker2_read_device_id(speaker2_t *ctx, speaker2_device_id_t *device_id)
Speaker 2 read device ID function.
err_t speaker2_chip_erase(speaker2_t *ctx)
Speaker 2 chip erase function.
err_t speaker2_set_clock_cfg(speaker2_t *ctx, uint8_t clk_cfg)
Speaker 2 set clock configuration function.
err_t speaker2_wait_ready(speaker2_t *ctx)
Speaker 2 wait ready function.
err_t speaker2_flash_write(speaker2_t *ctx, uint32_t address, uint8_t *data_in, uint16_t data_len)
Speaker 2 flash write function.
err_t speaker2_stop_loop_play(speaker2_t *ctx)
Speaker 2 stop loop play function.
speaker2_return_value_t
Speaker 2 Click return value data.
Definition speaker2.h:361
@ SPEAKER2_ERROR
Definition speaker2.h:363
@ SPEAKER2_OK
Definition speaker2.h:362
This file contains SPI specific macros, functions, etc.
Speaker 2 Click configuration object.
Definition speaker2.h:337
pin_name_t io5
Definition speaker2.h:346
spi_master_chip_select_polarity_t cs_polarity
Definition speaker2.h:352
pin_name_t sck
Definition speaker2.h:341
spi_master_mode_t spi_mode
Definition speaker2.h:351
pin_name_t mosi
Definition speaker2.h:340
uint32_t spi_speed
Definition speaker2.h:350
pin_name_t int_pin
Definition speaker2.h:347
pin_name_t rdy
Definition speaker2.h:345
pin_name_t miso
Definition speaker2.h:339
pin_name_t cs
Definition speaker2.h:342
Speaker 2 Click device ID object.
Definition speaker2.h:299
uint8_t part_id
Definition speaker2.h:300
uint8_t dev_id
Definition speaker2.h:303
uint8_t mem_type
Definition speaker2.h:302
uint8_t man_id
Definition speaker2.h:301
Speaker 2 Click context object.
Definition speaker2.h:312
spi_master_t spi
Definition speaker2.h:321
digital_out_t io5
Definition speaker2.h:314
digital_in_t int_pin
Definition speaker2.h:318
speaker2_device_id_t device_id
Definition speaker2.h:328
uint8_t status
Definition speaker2.h:326
pin_name_t chip_select
Definition speaker2.h:323
uint8_t cmd_payload[SPEAKER2_PAYLOAD_SIZE]
Definition speaker2.h:325
digital_in_t rdy
Definition speaker2.h:317